package com.winkyle.woodenfish.widget;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.winkyle.woodenfish.R;

/**
 * @author winkyle
 * @date 2018/06/15
 * @description 继承自 {@link LinearLayout}的底部工具条的tab项类
 */
public class BottomBarTab extends LinearLayout {
    private ImageView imageView;
    private ImageView remindimageView;
    private Context context;
    private TextView textView;
    private int tabPosition = -1;
    private int icon;
    private static boolean ifshow = false;
    /**
     * @param context 上下文
     * @param icon 图标id
     * @param title 标题
     */
    public BottomBarTab(Context context, @DrawableRes int icon, String title) {
        this(context, null, icon,  title);
    }


    /**
     * @param context 上下文
     * @param attrs 组件参数
     * @param icon 图标id
     * @param title 标题
     */
    public BottomBarTab(Context context, AttributeSet attrs, int icon, String title) {
        this(context, attrs, 0, icon, title);
    }

    /**
     *
     * @param context 上下文
     * @param attrs 组件参数
     * @param defStyleAttr 风格样式
     * @param icon 图标id
     * @param title 标题
     */
    public BottomBarTab(Context context, AttributeSet attrs, int defStyleAttr, int icon, String title) {
        super(context, attrs, defStyleAttr);
        init(context, icon, title);
    }

    /**
     * @description 将图片，文字加进tab项中
     * @param context 上下文
     * @param icon 图标id
     * @param title 标题
     */
    private void init(Context context, int icon, String title) {
        this.context = context;
        this.icon =icon;

        setOrientation(LinearLayout.VERTICAL);

        remindimageView = new ImageView(context);
        int remindSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
        LayoutParams remindparams = new LayoutParams(remindSize, remindSize);
        remindparams.gravity = Gravity.CENTER_HORIZONTAL;
        remindparams.topMargin = 2;
        setRemind(false);
        remindimageView.setLayoutParams(remindparams);

        imageView = new ImageView(context);
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
        LayoutParams params = new LayoutParams(size, size);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        params.topMargin = 2;
        imageView.setImageResource(icon);
        imageView.setLayoutParams(params);

        LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        textViewParams.gravity = Gravity.CENTER_HORIZONTAL;
        textViewParams.topMargin = 3;
        textViewParams.bottomMargin = 5;
        textView = new TextView(context);
        textView.setText(title);
        textView.setTextSize(11);
        textView.setLayoutParams(textViewParams);
        textView.setTextColor(ContextCompat.getColor(context, R.color.darkGrey));
        addView(remindimageView);
        addView(imageView);
        addView(textView);
    }

    /**
     * @description 选中时变化
     * @param selected
     */
    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            imageView.setColorFilter(ContextCompat.getColor(context, R.color.blue));
            textView.setTextColor(ContextCompat.getColor(context, R.color.blue));
        } else {
            imageView.setColorFilter(ContextCompat.getColor(context, R.color.darkGrey));
            textView.setTextColor(ContextCompat.getColor(context, R.color.darkGrey));
        }
    }

    /**
     * @description 设置位置
     * @param position
     */
    public void setTabPosition(int position) {
        tabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    /**
     * @description 获取位置
     * @return
     */
    public int getTabPosition() {
        return tabPosition;
    }

    /**
     * @description 设置是否提示
     * @param remind 是否提示
     * @return
     */
    public void setRemind(boolean remind) {
        if(remind) {
            remindimageView.setImageResource(R.drawable.red_circle);
            return;
        }
        remindimageView.setImageResource(R.drawable.white_circle);
    }
}
